<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index2.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-button type="primary" class="butT" @click="handleCreate()">新建一级菜单</el-button>
            </div>
            <el-table
                    :data="dataList"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    border
                    stripe
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        prop="name"
                        label="菜单名称"
                        width="180"
                        align="left">
                </el-table-column>
                <el-table-column
                        prop="priority"
                        label="优先级"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="linkUrl"
                        label="链接地址"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="icon"
                        label="菜单图标"
                        align="center">
                    <template slot-scope="scope">
                        <i :class="scope.row.icon"/>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="description"
                        label="说明"
                        align="center">
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" v-if="scope.row.parentMenuId == null" size="mini"
                                   @click="handleCreateM2(scope.row.name, scope.row.id)">新增子菜单
                        </el-button>
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" v-if="scope.row.children == null || scope.row.children.length == 0"
                                   type="danger" @click="handleDelete(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增一级菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddM1Form" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="6">
                                <el-form-item label="菜单图标">
                                    <el-select v-model="formData.icon" placeholder="请选择">
                                        <el-option value="el-icon-edit-tty">
                                            <i class="el-icon-edit"></i>
                                        </el-option>
                                        <el-option value="el-icon-s-tools">
                                            <i class="el-icon-s-tools"></i>
                                        </el-option>
                                        <el-option value="el-icon-user-solid">
                                            <i class="el-icon-user-solid"></i>
                                        </el-option>
                                        <el-option value="el-icon-message-solids">
                                            <i class="el-icon-message-solid"></i>
                                        </el-option>
                                        <el-option value="el-icon-s-data">
                                            <i class="el-icon-s-data"></i>
                                        </el-option>
                                        <el-option value="el-icon-s-comment">
                                            <i class="el-icon-s-comment"></i>
                                        </el-option>
                                        <el-option value="el-icon-s-platform">
                                            <i class="el-icon-s-platform"></i>
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAddM1()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <div class="add-form">
                <el-dialog title="新增子菜单" :visible.sync="dialogFormVisibleM2">
                    <el-form ref="dataAddM2Form" :rules="rules2" :model="formData" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="父菜单名称" prop="name">
                                    {{parentMenu.name}}
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="子菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="链接地址" prop="linkUrl">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleM2 = false">取消</el-button>
                        <el-button type="primary" @click="handleAddM2()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="优先级">
                                    <el-input v-model="formData.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24" v-if="formData.parentMenuId != null">
                                <el-form-item label="链接地址" prop="linkUrl"
                                              :rules="[{ required: true, message: '请输入链接地址', trigger: 'blur' }]">
                                    <el-input v-model="formData.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row v-if="formData.parentMenuId == null">
                            <el-col :span="6">
                                <el-form-item label="菜单图标"
                                              :rules="[{ required: true, message: '请选择一个菜单图标', trigger: 'blur' }]">
                                    <el-select v-model="formData.icon" placeholder="请选择">
                                        <el-option value="el-icon-edit">
                                            <i class="el-icon-edit"></i>
                                        </el-option>
                                        <el-option value="el-icon-s-tools">
                                            <i class="el-icon-s-tools"></i>
                                        </el-option>
                                        <el-option value="el-icon-user-solid">
                                            <i class="el-icon-user-solid"></i>
                                        </el-option>
                                        <el-option value="el-icon-message-solid">
                                            <i class="el-icon-message-solid"></i>
                                        </el-option>
                                        <el-option value="el-icon-s-data">
                                            <i class="el-icon-s-data"></i>
                                        </el-option>
                                        <el-option value="el-icon-s-comment">
                                            <i class="el-icon-s-comment"></i>
                                        </el-option>
                                        <el-option value="el-icon-s-platform">
                                            <i class="el-icon-s-platform"></i>
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="说明">
                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index7.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            dataList: [], //当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            dialogFormVisibleM2: false,
            parentMenu: {},
            rules: {//校验规则
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}],
                priority: [{required: true, message: '优先级为必填项', trigger: 'blur'}],
                icon: [{required: true, message: '请选择一个菜单图标', trigger: 'blur'}]
            },
            rules2: {//校验规则
                name: [{required: true, message: '菜单名称为必填项', trigger: 'blur'}],
                priority: [{required: true, message: '优先级为必填项', trigger: 'blur'}],
                linkUrl: [{required: true, message: '链接地址为必填项', trigger: 'blur'}]
            },
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            console.log(this.dataList)
            this.findAll();
        },
        methods: {
            handleCreateM2(name, id) {
                this.resetForm();
                this.parentMenu.name = name;
                this.formData.parentMenuId = id;
                this.dialogFormVisibleM2 = true;

            },
            handleAddM2() {
                // 提交添加子菜单
                this.$refs['dataAddM2Form'].validate((valid) => {
                    if (valid) {
                        // 所有检验通过, 提交数据给后台this.formData
                        axios.post('/menu/add.do', this.formData).then(res => {
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? "success" : "error"
                            })
                            if (res.data.flag) {
                                // 成功的处理
                                // 关闭窗口
                                this.dialogFormVisibleM2 = false;
                                // 刷新列表数据
                                this.findAll();
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //编辑
            handleEdit() {
                // 提交修改
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid) {
                        // 所有检验通过, 提交数据给后台this.formData
                        axios.post('/menu/update.do', this.formData).then(res => {
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? "success" : "error"
                            })
                            if (res.data.flag) {
                                // 成功的处理
                                // 关闭窗口
                                this.dialogFormVisible4Edit = false;
                                // 刷新列表数据
                                this.findAll();
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //添加1级菜单
            handleAddM1() {
                // 检验通过后才能提交
                //  ruleForm ref
                this.$refs['dataAddM1Form'].validate((valid) => {
                    if (valid) {
                        // 所有检验通过, 提交数据给后台this.formData
                        axios.post('/menu/add.do', this.formData).then(res => {
                            this.$message({
                                message: res.data.message,
                                type: res.data.flag ? "success" : "error"
                            })
                            if (res.data.flag) {
                                // 成功的处理
                                // 关闭窗口
                                this.dialogFormVisible = false;
                                // 刷新列表数据
                                this.findAll();
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            //分页查询
            findAll() {
                // 发送查询的请求，提交pagination(currentPage,pageSize)
                axios.get('/menu/findAllMenuAsTree.do').then(res => {
                    if (res.data.flag) {
                        this.dataList = res.data.data;
                        // 总计录数
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            // 重置表单
            resetForm() {
                // 清空表单
                this.formData = {};
                this.parentMenu = {};
            },
            // 弹出添加窗口
            handleCreate() {
                this.resetForm();
                // 弹出新增窗口
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.resetForm();
                var id = row.id;
                // 弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                // 回显 发送请求
                axios.get('/menu/findById.do?id=' + id).then(res => {
                    if (res.data.flag) {
                        // 绑定数据回显, 编辑的form表单也绑定了这个formData
                        this.formData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            // 删除
            handleDelete(row) {
                // row行数据，数据库中的一条记录，checkitem实体对象
                // 获取删除的id
                var id = row.id;
                //alert(JSON.stringify(row));
                this.$confirm('此操作将【永久删除】该菜单, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 点击确定后调用
                    axios.post('/menu/deleteById.do?id=' + id).then(res => {
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? "success" : "error"
                        })
                        if (res.data.flag) {
                            // 成功
                            // 刷新列表数据
                            this.findAll();
                        }
                    })
                }).catch(() => {
                    // 点击取消后调用
                    // 空着，防止报错
                });
            }
        }
    })
</script>
<style>
    .el-table__row--level-1 {
        background-color: #889aa4;
    }
</style>
</html>